/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vnetlab.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import vnetlab.elements.Computer;

import vnetlab.elements.Element;
import vnetlab.elements.Router;
import vnetlab.elements.Server;
import vnetlab.elements.Switch;


public class Manager {

    private static Manager instance;
    private Status status;
    private int maxID;
    private List<StatusListner> statusListners;

    private Map<String, Element> map ;
    
    private Manager() {
        map = new HashMap<String, Element>();
        status = Status.STOP;
        statusListners = new ArrayList<StatusListner>();
        maxID = 0;
    }

    public static Manager getInstance(){
        if(instance == null)
            instance = new Manager();

        return instance;
    }

    public void addElement(String id, String tipo, Computer cp,Router rt,Server sv, Switch sw){

        if(tipo.equalsIgnoreCase("ROUTER")){
            map.put(id, rt);
        }
        else if(tipo.equalsIgnoreCase("SWITCH")){
            map.put(id, sw);
        }
        else if(tipo.equalsIgnoreCase("SERVER")){
            map.put(id, sv);
        }
        else if(tipo.equalsIgnoreCase("COMPUTER")){
            map.put(id, cp);
        }

        maxID++;
    }

    public void addElement(Element element){
        map.put(element.getIdent(), element);
        maxID++;
    }

    public Map<String,Element> getMap(){
        return map;
    }

    public int getMaxID(){
        return maxID;
    }
    
    public void removeElement(String id){
        map.remove(id);
    }

    public void connectElements(String idSource, String idEnd){

        Element eSource = map.get(idSource);
        Element eEnd = map.get(idEnd);

        //Faz a magica de ligar os elementos
        //Que eu não tinha pensado como vai ser feito xD
    }

    public Element getElement(String id){
        return map.get(id);
    }

    public int getSize() {
        return map.size();
    }

    public void setStatus(final Status status) {
        this.status = status;
        
        new Thread( new Runnable() {
            
            public void run() {
                for(StatusListner sl: statusListners)
                    sl.statusModified(status);
                }
            }
        ).start();
    }

    public Status getStatus() {
        return status;
    }

    public void addStatusListner(StatusListner statusListner){
        statusListners.add(statusListner);
    }

    public void clear() {
        map = new HashMap<String, Element>();
        status = Status.STOP;
        maxID = 0;
        System.gc();
    }
}
